/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	liuyang 
 * 
 * <p>描   述  :   RegectTaskSingleBackWayCmd.java for com.pengtu.service.custom    </p>
 * 
 * <p>最后修改 : $: 2011-8-18-下午6:12:02 v 1.0.0	 liuyang   $     </p>
 * 
 */

package com.pengtu.service.custom;

import org.jbpm.api.TaskService;
import org.jbpm.api.cmd.Environment;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.model.ActivityImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.TransitionImpl;
import org.jbpm.pvm.internal.task.TaskImpl;

/**
 * 流程回退帮助类
 * 该驳回命令优点在于无需指定上一步骤的节点名称(previous activity)，只能用于那些只有一条返回路径的回退应用场景
 * 
 * RegectTaskSingleBackWayCmd
 * 
 * 2011-8-18 下午6:12:02
 * 
 * @version 1.0.0
 * 
 */
public class RegectTaskSingleBackWayCmd extends AbstractCommand<String> {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;

	private String taskId;

	public RegectTaskSingleBackWayCmd() {

	}

	public RegectTaskSingleBackWayCmd(String taskId) {
		this.taskId = taskId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jbpm.api.cmd.Command#execute(org.jbpm.api.cmd.Environment)
	 */
	@Override
	public String execute(Environment environment) throws Exception {
		// TODO Auto-generated method stub
		TaskService taskService = environment.get(TaskService.class);
		TaskImpl currentTask = (TaskImpl) taskService.getTask(taskId);
		ExecutionImpl exec = currentTask.getProcessInstance();
		// 获取当前任务的活动节点
		ActivityImpl currentActivity = exec.getActivity();
		// 获取该活动节点的IncomingTransition
		TransitionImpl incomingTransition = (TransitionImpl) currentActivity
				.getIncomingTransitions().get(0);
		// 获取IncomingTransition的源活动节点
		ActivityImpl sourceActivity = incomingTransition.getSource();

		TransitionImpl backTransition = currentActivity
				.createOutgoingTransition();

		// 当前活动节点为新的回退Transition的源，而原来的“源”活动节点变成了目标活动节点
		backTransition.setSource(currentActivity);
		backTransition.setDestination(sourceActivity);
		backTransition.setName("regect " + incomingTransition.getName());
		taskService.completeTask(taskId, backTransition.getName());
		return sourceActivity.getName();
	}

}
